<?php

namespace App\Http\Controllers\Api;

use App\Ad\Docking\DockingBase;
use App\Caches\AdPlanCache;
use App\Datalog\AdMonitorLog;
use App\Http\Controllers\Controller;
use App\Services\DataCache\AdPlanDockingDataCache;
use Illuminate\Http\Request;

class AdMonitorController extends Controller
{
    public function mediaMonitor(Request $request)
    {
        $plan_id = $request->input('plan_id') ?? 0;
        $media_docking_id = AdPlanDockingDataCache::getInstance()->get($plan_id);
        $docking = DockingBase::getClass($media_docking_id);
        if (!$docking) {
            return yqerr(6029);
        }

        return $docking->handleMonitorInfo($request);
    }

    public function monitorUrl(Request $request)
    {
        $plan_id = $request->input('plan_id') ?? 0;
        $plan    = AdPlanCache::getInstance()->remember($plan_id);
        if (empty($plan)) {
            return yqerr(6029);
        }

        $docking = DockingBase::getClass($plan['media_docking_id']);
        if (!$docking) {
            return yqerr(6029);
        }

        $monitor_url = $docking->getMonitorUrl(0, 0, ['plan_id' => $plan_id]);
        return yqok(compact('monitor_url'));
    }
}
